package com.studyyoun.android.nonghe.welcom.activity;
/**
 * Created by zhaolong on 2017/10/3.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.exce.OkhttpException;
import com.studyyoun.android.androidcommonbaselibrary.utils.AnimationUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.CommonLoadingUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.SharedPreferencesUtil;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

import java.util.concurrent.ConcurrentHashMap;

/**
 * class infation
 */
public class LoginActivity extends CommonBaseActivity {

    private TextView mHeaderTitleTextView;
    private LinearLayout mBackLinearLayout;
    private RelativeLayout mHeaderRelativeLayout;
    private TextView mWelcomeTextView;
    private LinearLayout mUserNameLinearLayout;
    private LinearLayout mPasswordLinearLayout;
    private RelativeLayout mForgetRelativeLayout;
    private Button mLoginButton;
    private TextView mCheckCodeTextView;
    private EditText mUserNameEditText;
    private EditText mPasswordEditText;
    private LoginReciver mLoginReciver;

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_login_layout;
    }

    @Override
    protected void commonInitView(View view) {

        mHeaderTitleTextView = findViewById(R.id.tv_base_header_title);
        mBackLinearLayout = findViewById(R.id.ll_base_back);
        mHeaderRelativeLayout = findViewById(R.id.rl_login_header);
        mHeaderRelativeLayout.setVisibility(View.INVISIBLE);

        mWelcomeTextView = findViewById(R.id.tv_login_header_title);
        mWelcomeTextView.setVisibility(View.INVISIBLE);

        mUserNameLinearLayout = findViewById(R.id.ll_login_username);
        mUserNameLinearLayout.setVisibility(View.INVISIBLE);
        mPasswordLinearLayout = findViewById(R.id.ll_login_password);
        mPasswordLinearLayout.setVisibility(View.INVISIBLE);

        mForgetRelativeLayout = findViewById(R.id.ll_login_foregt_pas);
        mForgetRelativeLayout.setVisibility(View.INVISIBLE);

        mLoginButton = findViewById(R.id.tv_commit_login_data);
        mLoginButton.setVisibility(View.INVISIBLE);

        //验证码登录
        mCheckCodeTextView = findViewById(R.id.et_login_to_regist_pas);

        mUserNameEditText = findViewById(R.id.et_login_username_input);

        mPasswordEditText = findViewById(R.id.et_login_password_input);
    }

    @Override
    protected void commonFunction() {
        mHeaderTitleTextView.setText("登录");
        mBackLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void commonDelayFunction() {

        mLoginReciver = new LoginReciver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("login_app");
        registerReceiver(mLoginReciver,intentFilter);

        AnimationUtils.initLeftToRightAnimationFunction(mWelcomeTextView,500,10);

        AnimationUtils.initLeftToRightAnimationFunction(mUserNameLinearLayout,500,200);
        AnimationUtils.initRightToLeftAnimationFunction(mPasswordLinearLayout,500,300);
        AnimationUtils.initAlphaAnimation(mForgetRelativeLayout,500,400);
        AnimationUtils.initAlphaAnimation(mLoginButton,500,500);
        AnimationUtils.initAlphaAnimation(mHeaderRelativeLayout,500,500);

        mCheckCodeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,RegistActivity.class));
            }
        });

        String userName = SharedPreferencesUtil.getInstance(mContext).getData("userName");
        mUserNameEditText.setText(userName);

        mUserNameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String string = charSequence.toString();
                if (!TextUtils.isEmpty(string)&&string.length()==11){
                    SharedPreferencesUtil.getInstance(mContext).saveData("userName",string);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = mUserNameEditText.getText().toString();
                String password = mPasswordEditText.getText().toString();
                if (TextUtils.isEmpty(userName)){
                    ToastUtils.show("请输入姓名",mContext);
                    return;
                }else if (TextUtils.isEmpty(password)){
                    ToastUtils.show("请输入标准身份证号",mContext);
                    return;
                }
                loginFunction(userName,password);
            }
        });
    }
    private long mPreTime ;
    /**
     * 短信登录功能
     * @param userName
     * @param password
     */
    private void loginFunction(String userName, String password) {


        mPreTime = System.currentTimeMillis();
        CommonLoadingUtils.getInstance().showLoading(mWelcomeTextView);
        ConcurrentHashMap<String, String> key = new ConcurrentHashMap<>();
        key.put("sfzcode",password);
        key.put("name",userName);
        final String url= HttpUrlHelper.sUserLoginUrl;
        OkhttpCommonClient.sentPostRequest(url, key, new OkhttpCommonCallBack(UserModel.class) {
            @Override
            public void onSuccess(final Object obj) {

                long flagTime = System.currentTimeMillis()-mPreTime;
                if (flagTime < 1500) {
                    flagTime = 1500 - flagTime;
                } else {
                    flagTime = 0;
                }
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        CommonLoadingUtils.getInstance().closeFunction();
                        if (obj != null) {
                            UserModel userModel = (UserModel) obj;

                            UserModel.DataBean lData = userModel.getData();
                            if (lData != null) {
                                CommonUserHelper.saveUserData(userModel);
                                ToastUtils.show("登录成功", mContext);
                                LoginActivity.this.finish();
                            }else{
                                ToastUtils.show("登录失败:身份验证失败", mContext);
                            }


                        } else {
                            ToastUtils.show("登录失败 数据异常", mContext);
                        }
                    }
                }, flagTime);

            }

            @Override
            public void onFaile(Object obj) {
                OkhttpException exception = (OkhttpException) obj;

                ToastUtils.show("登录失败:身份验证失败", mContext);
                CommonLoadingUtils.getInstance().closeFunction();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLoginReciver);
    }

    private class LoginReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (TextUtils.equals(action,"login_app")){
                LoginActivity.this.finish();
            }
        }
    }
}
